#include "Node.h"

#define GRAVITY Vector3f(0.0f, -9.8f, 0.0f)

Node::Node(Vector3f pos, float mass) {
	position = prevpos = pos;
	velocity = Vector3f();
	acceleration = Vector3f();

	m = mass;
}

Node::~Node() {
}

void Node::Update(float frameTime) {
	Vector3f resultForce = Vector3f();
	Vector3f gravForce = GRAVITY * m;
	Vector3f structResult = Vector3f();
	for(size_t i = 0; i < structural.size(); i++) {
		Structural* structSpring = structural[i];

		structResult += structSpring->getForce();
	}

	// change to ms
	frameTime /= CLOCKS_PER_SEC;

	prevpos = position;

	resultForce = structResult + gravForce;
	acceleration = resultForce / m;
	velocity += acceleration * frameTime;
	position += (velocity * frameTime);

	if(position.y < 0)
		position.y = 0;
}

void Node::AddStructural(Structural *spring) {
	structural.push_back(spring);
}

Vector3f Node::getPosition() const{
	return position;
}

Vector3f Node::getVelocity() const{
	return velocity;
}